package com.lg.desireplayer.member;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.lg.desireplayer.R;

public class Signup extends Activity {

	private Button signup_button_signup;
	private EditText signup_id, signup_pw, signup_pwCheck, signup_email;
	private boolean flag = false;
	private int[] checkSum = { 0, 0, 0, 0, 0 };
	private CheckBox signup_isBand;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);

		signup_id = (EditText) findViewById(R.id.Signup_Edit_id);
		signup_pw = (EditText) findViewById(R.id.Signup_Edit_pw);
		signup_pwCheck = (EditText) findViewById(R.id.Signup_Edit_pwCheck);
		signup_email = (EditText) findViewById(R.id.Signup_Edit_email);
		signup_isBand = (CheckBox) findViewById(R.id.Signup_checkBox_admin);

		signup_button_signup = (Button) findViewById(R.id.Signup_Button_signup);
		signup_button_signup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (signup_id.getText().toString().equals("")) {
					checkSum[0] = 0;
					signup_id.setHintTextColor(Color.parseColor("#F15F5F"));
				} else {
					checkSum[0] = 1;
				}

				if (signup_pw.getText().toString().equals("")) {
					checkSum[1] = 0;
					signup_pw.setHintTextColor(Color.parseColor("#F15F5F"));
				} else {
					checkSum[1] = 1;
				}

				if (signup_pwCheck.getText().toString().equals("")) {
					checkSum[2] = 0;
					signup_pwCheck.setHintTextColor(Color.parseColor("#F15F5F"));
				} else {
					checkSum[2] = 1;
				}

				if (signup_email.getText().toString().equals("")) {
					checkSum[3] = 0;
					signup_email.setHintTextColor(Color.parseColor("#F15F5F"));
				} else {
					checkSum[3] = 1;
				}

				if (!(signup_pw.getText().toString().equals(signup_pwCheck
						.getText().toString()))) {

					Toast.makeText(Signup.this, "패스워드 불일치", Toast.LENGTH_SHORT)
							.show();
					checkSum[4] = 0;
				} else {
					checkSum[4] = 1;
				}

				int sum = 0;
				for (int i = 0; i < checkSum.length; i++) {
					sum += checkSum[i];
				}

				if (sum == checkSum.length) {

					String resultId = null;
					String resultPw = null;
					String resultCheck = null;
					StringBuffer str1 = new StringBuffer(
							"http://192.168.0.131:8080/com.lg.project.member.cmd.MemberLoginCmd.vcc?username=");
					str1.append(signup_id.getText().toString());
					str1.append("&password=");
					str1.append(signup_pw.getText().toString());

					try {
						URL url1 = new URL(str1.toString());

						XmlPullParserFactory factory = XmlPullParserFactory
								.newInstance();
						XmlPullParser parser = factory.newPullParser();
						parser.setInput(url1.openStream(), "utf-8");

						int eventType = parser.getEventType();
						while (eventType != XmlPullParser.END_DOCUMENT) {
							switch (eventType) {
							case XmlPullParser.START_DOCUMENT:
								break;
							case XmlPullParser.START_TAG:
								if (parser.getName().equals("username")) {
									resultId = parser.nextText();
								}
								break;
							case XmlPullParser.END_TAG:
								break;
							}
							eventType = parser.next();
						}

					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}

					if (signup_id.getText().toString().equals(resultId)) {
						Toast.makeText(Signup.this, "이미 가입한 ID 입니다.",
								Toast.LENGTH_SHORT).show();
					} else {
						StringBuffer str = new StringBuffer(
								"http://192.168.0.131:8080/com.lg.project.member.cmd.MemberSignupCmd.vcc?username=");

						str.append(signup_id.getText().toString());
						str.append("&password=");
						str.append(signup_pw.getText().toString());
						str.append("&email=");
						str.append(signup_email.getText().toString());
						str.append("&isband=");
						if (signup_isBand.isChecked()) {
							str.append(1);
						} else {
							str.append(0);
						}
						try {
							URL url = new URL(str.toString());
							url.openStream();
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}

//						Intent intent = new Intent(Signup.this, Login.class);
//						startActivity(intent);
						overridePendingTransition(R.anim.rightin,
								R.anim.rightout);
						finish();
					}

				} else {
					for (int i = 0; i < checkSum.length; i++) {
						checkSum[i] = 0;
					}
				}
			}
		});

	}
}
